<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "wmtg_coupon_category".
 *
 * @property int $id
 * @property string $title 优惠券名称
 * @property string $mark_id 标识ID
 * @property int $group_id 券号
 * @property int $origin_num 优惠券发放数量
 * @property int $use_num 优惠券领取数量
 * @property int $status 状态
 * @property int $created_at 优惠券创建时间
 * @property int $updated_at 更新时间
 * @property string $local_mark_id 本地标识
 * @property int $type 类型
 * @property string $image 优惠券图片
 */
class CouponCategory extends \yii\db\ActiveRecord
{
    const STATUS_ON = 1;
    const STATUS_OFF = 0;
    const TYPE_NUM_MANY=2;
    const TYPE_NUM_ONE=1;
    public $pic;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'wmtg_coupon_category';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'mark_id','group_id', 'status','type'], 'required'],
            [['group_id', 'origin_num', 'use_num', 'status', 'created_at', 'updated_at','type'], 'integer'],
            [['title'], 'string', 'max' => 100],
            [['mark_id'], 'string', 'max' => 50],
            [['local_mark_id','image'], 'string', 'max' => 255],
            [['pic'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                return !empty($model->pic);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '优惠券名称',
            'mark_id' => '券号',
            'group_id' => 'group_id',
            'origin_num' => '优惠券发放数量',
            'use_num' => '优惠券领取数量',
            'status' => '状态',
            'created_at' => '优惠券创建时间',
            'updated_at' => '优惠券更新时间',
            'local_mark_id' => '本地标识',
            'type' => '类型',
            'image'=>'优惠券图片',
    'pic'=>'优惠券图片'
        ];
    }
    public function objToArr()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'mark_id' => $this->mark_id,
            'group_id' => $this->group_id,
            'origin_num' => $this->origin_num,
            'use_num' => $this->use_num,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'local_mark_id' => $this->local_mark_id,
            'type' => $this->type,
            'image' => $this->image
        ];
    }
    public function afterDelete()
    {
        $collection = self::getCollection();
        $collection->remove(['id' => $this->id]);
        return true;
    }

    public function afterSave($insert, $changedAttributes)
    {
        $collection = self::getCollection();
        $find = $collection->findOne(['id' => $this->id]);
        if ($find) {
            $collection->update(['id' => $this->id], $this->objToArr());
        } else {
            $collection->insert($this->objToArr());
        }
        return true;
    }


    public static function getCollection()
    {
        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase();
        return $collection = $database->getCollection("coupon_category");
    }
    public function upload(){
        if($this->validate('pic')){
//            $imgname='banner_'.date('YmdHis').rand(1000,9999);
//            $this->pic->saveAs(Yii::$app->basePath.'/web/uploads/banner/'.$imgname.'.'.$this->pic->extension);
//            return '/admin/uploads/banner/'.$imgname.'.'.$this->pic->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/couponcategory/';
            $filename = uniqid().'.'.$this->pic->extension;

            $stream = fopen($this->pic->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;


        }else{
            return false;
        }

    }
}
